#include <bits/stdc++.h>
// 2025/01/04
// tag:
using namespace std;
#define int long long
const int N = 251;
#define F first
#define S second
int n, a[N][N], id[N][N], arid, clk;

struct G
{
    map<int, set<int>> g;
    map<int, int> nsize, rid, rsize;
};

map<int, G> g1;
map<pair<int, int>, G> g2;

inline int dfs(G &g, int xid, int yid)
{
    if (g.rid.count(xid) > 0)
        return 0;
    g.rid[xid] = yid;
    int ans = g.nsize[xid];
    for (auto i : g.g[xid])
        ans += dfs(g, i, yid);
    g.rsize[yid] = ans;
    return ans;
}

inline int LargestR(G &g)
{
    int l = 0;
    for (auto &i : g.g)
        l = max(l, dfs(g, i.F, ++arid));
    return l;
}

inline void addEdge(G &g, int a, int b)
{
    g.g[a].insert(b), g.g[b].insert(a), g.nsize[a] = g.nsize[b] = 1;
}

void addEBR(int x1, int y1, int x2, int y2)
{
    int id1 = id[x1][y1], id2 = id[x2][y2], c1 = a[x1][y1], c2 = a[x2][y2];
    if (c1 > c2)
        swap(c1, c2), swap(id1, id2);
    int r1 = g1[c1].rid[id1], r2 = g1[c2].rid[id2];
    pair<int, int> p = pair<int, int>(c1, c2);
    addEdge(g2[p], r1, r2);
    g2[p].nsize[r1] = g1[c1].rsize[r1];
    g2[p].nsize[r2] = g1[c2].rsize[r2];
}

signed main()
{
    freopen("multimoo.in", "r", stdin);
    freopen("multimoo.out", "w", stdout);
    cin >> n;
    for (int i = 1; i <= n; i++)
        for (int j = 1; j <= n; j++)
            cin >> a[i][j], id[i][j] = ++clk;
    for (int i = 1; i <= n; i++)
        for (int j = 1; j <= n; j++)
        {
            addEdge(g1[a[i][j]], id[i][j], id[i][j]);
            if (i < n && a[i + 1][j] == a[i][j])
                addEdge(g1[a[i][j]], id[i][j], id[i + 1][j]);
            if (j < n && a[i][j + 1] == a[i][j])
                addEdge(g1[a[i][j]], id[i][j], id[i][j + 1]);
        }
    int l1 = 0;
    for (auto &i : g1)
        l1 = max(l1, LargestR(i.S));
    for (int i = 1; i <= n; i++)
        for (int j = 1; j <= n; j++)
        {
            if (i < n && a[i + 1][j] != a[i][j])
                addEBR(i, j, i + 1, j);
            if (j < n && a[i][j + 1] != a[i][j])
                addEBR(i, j, i, j + 1);
        }
    int l2 = 0;
    for (auto &i : g2)
        l2 = max(l2, LargestR(i.S));
    cout << l1 << "\n" << l2 << endl;
    return 0;
}